var MJProto           = require('./MJProto');
cc.Class({
	extends: cc.Component,
	properties: {
		MaJiangAtlas: cc.SpriteAtlas,
		cardNode: cc.Node,
		bureauLabel: cc.Label,
	},

	onLoad: function () {
		this.reviewRecord = this.dialogParameters.reviewRecord;
		let lastItem = this.reviewRecord[this.reviewRecord.length-1];
		if (lastItem && !lastItem.result) { /* 最后一局未完成，删除 */ 
			this.reviewRecord.pop();
		}
		this.bureau = 0;
		this.showResult();
	},

	nextResult: function () {
		Global.AudioManager.playCommonSoundClickButton();
		let curBureau = this.bureau;
		if (this.bureau+1 >= this.reviewRecord.length) {
			return;
		}
		++ this.bureau;
		this.showResult();
	},

	preResult: function () {
		Global.AudioManager.playCommonSoundClickButton();
		let curBureau = this.bureau;
		if (this.bureau-1 < 0) {
			return;
		}
		-- this.bureau;
		this.showResult();
	},

	showResult: function () {
		this.bureauLabel.string = this.bureau+1;
		let data = this.reviewRecord[this.bureau];
		let pengGangArrays = []; /* 碰杠的牌 */ 
		for (let i = 0; i < data.handCards.length; ++i) {
			pengGangArrays.push([]);
		}
		for (let i = 0; i < data.operateRecord.length; ++i) {
			let item = data.operateRecord[i];
			if (item.operate == MJProto.operateType.GANG_ZHI) {
				pengGangArrays[item.chairID].push(item);
			}
			else if (item.operate == MJProto.operateType.GANG_BU) {
				for (let j = 0; j < pengGangArrays[item.chairID].length; ++j) {
					if (pengGangArrays[item.chairID][j].operate == MJProto.operateType.PENG && pengGangArrays[item.chairID][j].card == item.card) {
						pengGangArrays[item.chairID].splice(j, 1);
						break;
					}
				}
				pengGangArrays[item.chairID].push(item);
			}
			else if (item.operate == MJProto.operateType.GANG_CHI) {
				for (let j = 0; j < pengGangArrays[item.chairID].length; ++j) {
					if (pengGangArrays[item.chairID][j].operate == MJProto.operateType.PENG && pengGangArrays[item.chairID][j].card == item.card) {
						pengGangArrays[item.chairID].splice(j, 1);
						break;
					}
				}
				pengGangArrays[item.chairID].push(item);
			}
			else if (item.operate == MJProto.operateType.PENG) {
				pengGangArrays[item.chairID].push(item);
			}
		}
		let cardsLayout = cc.find('Layout', this.node);
		for (let node of cardsLayout.children) {
			node.destroy();
		}
		let posy = 350, itemHeight = 130;
		posy -= itemHeight/2;
		if (this.handItemNodeArray) {
			for (let node of this.handItemNodeArray) {
				node.destroy();
			}
		}
		this.handItemNodeArray = [];
		for (let i = 0; i < data.result.handCards.length; ++i) {
			if (!data.result.handCards[i]) { continue; }
			let node = this.getHandCardsItem(
				data.userArray[i],
				data.result.handCards[i],
				pengGangArrays[i],
				data.result.scores[i],
				(data.result.winChairIDArray.indexOf(i) != -1)? data.result.myMaCards:null,
				this.getArrayItemCount(i, data.result.fangGangArray),
				data.result.huType
			);
			node.parent = this.node;
			node.y = posy;
			posy -= itemHeight;
			this.handItemNodeArray.push(node);
		}
		posy += 60;

		/* 码牌 */
		let myMaCards = data.result.myMaCards;
		if (myMaCards && myMaCards.length > 0) {
			posy -= 20;
			let maLabel = cc.find('MaLabel', this.node);
			maLabel.active = true;
			maLabel.y = posy;
			posy += 20;
			let maLayout = cc.find('ZhongMa', this.node);
			for (let node of maLayout.children) {
				node.destroy();
			}
			maLayout.active = true;
			maLayout.y = posy;
			for (let i = 0; i < myMaCards.length; ++i) { /*马牌*/ 
				let node = this.getCardNode(myMaCards[i].card);
				cc.find('choosed', node).active = myMaCards[i].win;
				node.parent = maLayout;
				node.x = 18+37*i;
			}
			posy -= 70;
		}

		/* 剩余的牌 */
		posy -= 20;
		let restLabel = cc.find('RestLabel', this.node);
		restLabel.y = posy;
		let restCards = data.result.restCards;
		let restLayout = cc.find('RestCards', this.node);
		restLayout.y = posy + 20;
		for (let node of restLayout.children) {
			node.destroy();
		}
		for (let i = 0; i < restCards.length; ++i) {
			this.getCardNode(restCards[i]).parent = restLayout;
		}
		cc.find('close', this.node).zIndex = 2;
	},

	getHandCardsItem: function (user, handCards, pengGangArray, score, myMaCards, fangGangCount, huType) {
		let item = cc.instantiate(cc.find('Item', this.node));
		cc.find('name', item).getComponent(cc.Label).string = user.nickname;
		cc.find('uid', item).getComponent(cc.Label).string = user.uid;
		Global.CCHelper.updateSpriteFrame(user.avatar, cc.find('head', item).getComponent(cc.Sprite));
		cc.find('banker', item).active = user.isBanker;

		let layout = cc.find('Layout', item);
		let posx = 0, cardWidth = 37, nullWidth = 10;
		posx += cardWidth/2;
		if (pengGangArray.length > 0) {
			for (let i = 0; i < pengGangArray.length; ++i) {
				let obj = pengGangArray[i];
				let count = (MJProto.operateType.PENG == obj.operate)? 3:4;
				let sumx = 0;
				for (let j = 0; j < count; ++j) {
					let card = (obj.operate == MJProto.operateType.GANG_ZHI && j < 3)? 0:obj.card;
					let node = this.getCardNode(card);
					node.parent = layout;
					if (count == 4 && j == 3) {
						node.y = 10;
						node.x = posx-2*cardWidth;
					}
					else {
						node.x = posx;
						posx += cardWidth;
					}
					sumx += node.x;
				}
				if (count == 4) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.active = true;
					label.parent = item;
					label.x = sumx/4+layout.x;
					if (obj.operate == MJProto.operateType.GANG_ZHI) { /* 暗杠 */ 
						label.getComponent(cc.Label).string = '暗杠';
					}
					else if (obj.operate == MJProto.operateType.GANG_BU) { /* 明杠 */ 
						label.getComponent(cc.Label).string = '明杠';
					}
					else if (obj.operate == MJProto.operateType.GANG_CHI) { /* 接杠 */ 
						label.getComponent(cc.Label).string = '接杠';
					}
				}
				posx += nullWidth;
			}
		}

		if (myMaCards && myMaCards.length > 0) {
			cc.find('icon', item).active = true;
			for (let i = 0; i < handCards.length-1; ++i) {
				let node = this.getCardNode(handCards[i]);
				node.parent = layout;
				node.x = posx;
				posx += cardWidth;
				if (fangGangCount > 0 && i == handCards.length-2) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.parent = item;
					label.active = true;
					label.getComponent(cc.Label).string = (fangGangCount == 1)? '放杠':'放杠x'+fangGangCount;
					label.x = node.x+layout.x;
				}
			}
			posx += nullWidth;
			let huCardNode = this.getCardNode(handCards[handCards.length-1]);
			huCardNode.parent = layout;
			huCardNode.x = posx;
			posx += cardWidth;
			let huNode = cc.find('hu', item);
			huNode.parent = huCardNode;
			huNode.active = true;
			huNode.setPosition(18, 25);
			posx += nullWidth;
			let resultLabel = cc.find('resultLabel', item);
			resultLabel.active = true;
			resultLabel.getComponent(cc.Label).string = (huType == MJProto.operateType.HU_ZHI)? '自摸':'抢杠';
		}
		else {
			for (let i = 0; i < handCards.length; ++i) {
				let node = this.getCardNode(handCards[i]);
				node.parent = layout;
				node.x = posx;
				posx += cardWidth;
				if (fangGangCount > 0 && i == handCards.length-1) {
					let label = cc.instantiate(cc.find('malabel', item));
					label.parent = item;
					label.active = true;
					label.getComponent(cc.Label).string = (fangGangCount == 1)? '放杠':'放杠x'+fangGangCount;
					label.x = node.x+layout.x;
				}
			}
		}

		if (score > 0) {
			let label = cc.find('win', item).getComponent(cc.Label);
			label.node.active = true;
			label.string = '+' + parseInt(score);
		}
		else {
			let label = cc.find('lose', item).getComponent(cc.Label);
			label.node.active = true;
			label.string = parseInt(score);
		}
		item.active = true;
		return item;
	},

	getCardNode: function (card) {
		let node = cc.instantiate(this.cardNode);
		card = (card <= 9)? '0'+card:''+card;
		node.active = true;
		node.y = 0;
		let name = "down2_"+Math.floor(card/10)+"_"+(card%10);
		node.getComponent(cc.Sprite).spriteFrame = this.MaJiangAtlas.getSpriteFrame(name);
		return node;
	},

	getNullNode: function () {
		let node = new cc.Node();
		node.width = 20;
		return node;
	},

	close: function () {
		Global.DialogManager.destroyDialog("Game/Majiang/MJReviewResult");
	},

	getArrayItemCount: function (item, array) {
		let count = 0;
		if (array && array.length > 0) {
			for (let i = 0; i < array.length; ++i) {
				if (array[i] == item) {
					++ count;
				}
			}
		}
		return count;
	},
});
